package com.nttuan.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nttuan.mycalender.R;

public class SelectView extends LinearLayout{
	
	private final int DISTANCE = 100;
	private int iPostUp = 0;
	private int iPostDown = 0;
	private int	iIndex = 0;
	private int iLength = 0;
	private int iInLeft =0 , iInRight = 0,iOutLeft =0 , iOutRight = 0;;
	private String[] arrText;
	private Context mContext;
	private onChaneListener mListenner = null;
	private TextView textView;
    private boolean enable = true;
	public SelectView(Context context,AttributeSet attr) {
		super(context,attr);
		this.mContext = context;
		init();
        setAnimtion(R.anim.in_left,R.anim.in_right,R.anim.out_left,R.anim.out_right);
        setData(context.getResources().getStringArray(R.array.duringType));
	}
	private void init()
	{
		this.textView = new TextView(mContext);
		this.textView.setTextSize(16);
		this.textView.setTextColor(mContext.getResources().getColor(R.color.text_pri));
		this.textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		this.textView.setGravity(Gravity.CENTER);
	}
	public void setAnimtion(int inLeft, int inRight,int outLeft, int outRight)
	{
		this.iInLeft = inLeft;
		this.iInRight = inRight;
		this.iOutLeft = outLeft;
		this.iOutRight = outRight;
	}
	public void setData(String[] arr)
	{
		this.iLength = arr.length;
		this.arrText = arr;
		this.iIndex = 0;
		this.textView.setText(arrText[iIndex]);
		this.addView(textView);
	}

    public void setMyEnable(boolean enable) {
        this.enable = enable;
    }

    public void setText(String text)
	{
		for(int i =0; i < this.iLength; i++)
			if(arrText[i].equals(text))
			{
				iIndex = i;
                setView(0);
                return;
			}
	}
	public String getText()
	{
		try
		{
			return arrText[iIndex];
		}
		catch(Exception e)
		{return null;}
	}
	private void removeView(int idAnim)
	{
		if(idAnim != 0)
		{
			Animation anim = AnimationUtils.loadAnimation(mContext, idAnim);
			this.textView.startAnimation(anim);

		}
		this.removeAllViews();
	}
	private void setView(int idAnim)
	{
    //    removeView(R.anim.in_left);
		this.removeAllViews();
		this.textView.setText(arrText[iIndex]);
		this.addView(textView);
		if(idAnim != 0)
		{
			Animation anim = AnimationUtils.loadAnimation(mContext, idAnim);
			this.textView.startAnimation(anim);
		}
	}
	public void setChaneListener(onChaneListener listener)
	{
		this.mListenner = listener;
	}
	public interface onChaneListener
	{
		public void onTouchUp();
		public void onTouchDow();
	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) 
	{
        if(!enable)
            return true;
		int action = ev.getAction();
		if(action == MotionEvent.ACTION_DOWN)
		{
			iPostDown =(int)ev.getX();
			if(mListenner == null)
				return true;
			mListenner.onTouchDow();
			return true;
		}
		if(action == MotionEvent.ACTION_UP)
		{
			iPostUp =(int) ev.getX();
//			if(mListenner != null)
//				mListenner.onTouchUp();
//            else {
//                slideToRight();
//            }
		}
		if(Math.abs(iPostUp - iPostDown) >= DISTANCE && action == MotionEvent.ACTION_UP)
		{
			Log.d("distance", Math.abs(iPostUp - iPostDown)+"");
			if(iPostDown < iPostUp) { // to right
                slideToRight();
			}
			else{
                slideToLeft();
			}
		}else
            if(action == MotionEvent.ACTION_UP){
                slideToRight();
            }

		return true;
	}
    private boolean slideToRight(){
        if(++iIndex == iLength)
            iIndex  = 0;
        setView(iInLeft);
        return true;
    }
    private boolean slideToLeft(){
        if(--iIndex < 0)
            iIndex = iLength -1;
        setView(iInRight);
        return true;
    }
}
